package org.linlinjava.litemall.db.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.db.dao.LitemallServiceMapper;
import org.linlinjava.litemall.db.domain.LitemallBrand;
import org.linlinjava.litemall.db.domain.LitemallService;
import org.linlinjava.litemall.db.domain.LitemallServiceExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallServiceService {
    private final Log logger = LogFactory.getLog(LitemallServiceService.class);

    @Resource
    LitemallServiceMapper dao;
    @Autowired
    TUserService userService;

    public void insert(LitemallService service) {
        dao.insertSelective(service);
    }

    public void insert(LitemallBrand brand) {
        LitemallService service = new LitemallService();
        service.setUsername(brand.getUsername());
        service.setPassword(brand.getPassword());
        service.setAvatar(brand.getPicUrl());
        service.setRole("1");
        service.setBelong(brand.getId());
        service.setCreateTime(LocalDateTime.now());
        service.setUpdateTime(LocalDateTime.now());
        userService.insert(service);
        insert(service);
    }

    public void update(LitemallService service) {
        dao.updateByPrimaryKeySelective(service);
    }

    public void updateByExample(LitemallService service, LitemallServiceExample example) {
        dao.updateByExampleSelective(service, example);
    }

    public void updatePrimaryByExample(LitemallService service) {
        LitemallServiceExample example = new LitemallServiceExample();
        example.or().andBelongEqualTo(service.getBelong()).andRoleEqualTo("1");
        dao.updateByExampleSelective(service, example);
    }

    public void update(LitemallBrand brand) {
        LitemallService service = new LitemallService();
        service.setUsername(brand.getUsername());
        service.setPassword(brand.getPassword());
        service.setAvatar(brand.getPicUrl());
        service.setBelong(brand.getId());
        service.setUpdateTime(LocalDateTime.now());
        updatePrimaryByExample(service);
    }

    public void deletedByBrand(Integer brandId) {
        LitemallServiceExample example = new LitemallServiceExample();
        example.or().andBelongEqualTo(brandId);
        LitemallService service = new LitemallService();
        service.setIsDeleted(true);
        updateByExample(service, example);
    }


    public Integer getBelong(Integer id) {
        try {
            LitemallService litemallService = dao.selectByPrimaryKey(id);
            return litemallService.getBelong();
        } catch (NullPointerException e) {
            logger.error("NullPointer: id = " + id);
            return null;
        }
    }

    public LitemallService selectById(Integer userid) {
        return dao.selectByPrimaryKey(userid);
    }

    public void delete(int id) {
        dao.deleteByPrimaryKey(id);
    }

    public List<LitemallService> selectByBrandId(Integer brandId) {
        LitemallServiceExample example = new LitemallServiceExample();
        example.or().andBelongEqualTo(brandId);
        return dao.selectByExample(example);
    }

    public LitemallService getDefaultService(Integer brandId) {
        LitemallServiceExample example = new LitemallServiceExample();
        example.or().andBelongEqualTo(brandId);
        List<LitemallService> litemallServices = dao.selectByExample(example);
        for (LitemallService litemallService : litemallServices) {
            if (litemallService.getImType()!=null) {
                if (litemallService.getImType() == 0) {
                    return litemallService;
                }
            }
        }
        LitemallServiceExample example2 = new LitemallServiceExample();
        example2.or().andBelongEqualTo(brandId).andImTypeIsNull();
        return dao.selectOneByExample(example);
    }

}
